#!/usr/bin/env python
# encoding: utf-8
from blogpy.blog.models import *

def link():
    link = BlogRoll.objects.all()
    return link

def tags():
    tags = Tag.objects.all()
    return tags

def catalog():
    catalog = Catalog.objects.all()
    return catalog

def rcomments():
    latest_comment_list = Comment.objects.all().order_by('-comment_time')[0:5]
    return latest_comment_list

def archives():
    sql = r"select distinct DATE_FORMAT(pub_date,'%%Y,%%m,%%M') from blog_post order by pub_date desc"
    from django.db import connection
    cursor = connection.cursor()
    cursor.execute(sql)
    archiveList = cursor.fetchall()
    cursor.close()
    archiveList_new = [str.split(a[0],',') for a  in archiveList]
    return archiveList_new

def creatWidget():
    data_dic = {}
    widgetlist = Widget.objects.order_by('id')[0:2]
    for i in xrange(0,len(widgetlist)):
        widget_tags = []
        wideget = Widget.objects.order_by('id')[i].widgetModels.all()
        for w in wideget:
            func_name = '%s'%(w.modelfile)
            try:
                #data = eval(func_name+'()')
                data = globals()[func_name]()
                data_dic[func_name] = data
            except:
                pass
            widget_tags.append('widget/app/%s.html'%func_name)
        if i == 0:
            data_dic['widgetTagsleft'] = widget_tags
        else:
            data_dic['widgetTagsright'] = widget_tags
    return data_dic

